import React, { useEffect, useState } from 'react'
import { Redirect, Route, Switch, withRouter } from 'dva/router'
import axios from 'axios'
import Home from '../../routes/sandbox/home/Home'
import UserList from '../../routes/sandbox/user-manage/UserList'
import RightList from '../../routes/sandbox/right-manage/RightList'
import RoleList from '../../routes/sandbox/right-manage/RoleList'
import NewsAdd from '../../routes/sandbox/news-manage/NewsAdd'
import NewsDraft from '../../routes/sandbox/news-manage/NewsDraft'
import NewsCategory from '../../routes/sandbox/news-manage/NewsCategory'
import NewsPreview from '../../routes/sandbox/news-manage/NewsPreview'
import NewsUpdate from '../../routes/sandbox/news-manage/NewsUpdate'
import Audit from '../../routes/sandbox/audit-manage/Audit'
import AuditList from '../../routes/sandbox/audit-manage/AuditList'
import Unpublished from '../../routes/sandbox/publish-manage/Unpublished'
import Published from '../../routes/sandbox/publish-manage/Published'
import Sunset from '../../routes/sandbox/publish-manage/Sunset'
import NoPermission from '../../routes/sandbox/nopermission/NoPermission'

// import { connect } from 'dva'

function NewsRouter() {
    const localRouterMap = {
        // 首页
        '/home': Home,
        // 用户管理
        '/user-manage/list': UserList,
        // 权限管理
        '/right-manage/role/list': RoleList,
        '/right-manage/right/list': RightList,
        // 新闻管理
        "/news-manage/add": NewsAdd,
        "/news-manage/draft": NewsDraft,
        "/news-manage/category": NewsCategory,
        "/news-manage/preview/:id": NewsPreview,
        "/news-manage/update/:id": NewsUpdate,
        // 审核管理
        "/audit-manage/audit": Audit,
        "/audit-manage/list": AuditList,
        // 发布管理
        "/publish-manage/unpublished": Unpublished,
        "/publish-manage/published": Published,
        "/publish-manage/sunset": Sunset,


    }
    const { role: { rights } } = JSON.parse(localStorage.getItem('token'))
    const [BackRouteList, setBackRouteList] = useState([])

    useEffect(() => {
        Promise.all([
            axios.get('/rights'),
            axios.get('/children')
        ]).then(res => {
            setBackRouteList([...res[0].data, ...res[1].data])
        })
    }, [])
    const checkRoute = (item) => {
        // 是否存(创建)在路径(组件)且开关pagepermisson是否打开
        return localRouterMap[item.key] && (item.routepermisson || item.pagepermisson)

    }
    const checkUserPermission = (item) => {
        // 拦截权限
        return rights.includes(item.key)
    }
    return (
        <Switch>
            {
                BackRouteList.map(item => {
                    if (checkRoute(item) && checkUserPermission(item)) {
                        return <Route path={item.key} key={item.key}
                            component={localRouterMap[item.key]} exact />
                    }
                    return null
                })

            }
            <Redirect from='/' to='/home' exact />
            {
                BackRouteList.length > 0 && <Route path='*'
                    component={NoPermission} />
            }


        </Switch>
    )
}

export default withRouter(NewsRouter)